package cn.edu.njtc.controller;

import cn.edu.njtc.entity.pojo.QRCodeGenerator;
import cn.edu.njtc.service.QRCodeGeneratorService;
import cn.edu.njtc.util.QRCodeDisplayUtil;
import com.google.zxing.WriterException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.io.IOException;

@RestController
@RequestMapping("/qr-code")
@Tag(name = "二维码生成接口")
public class QRCodeGeneratorController {

    private static final Logger logger = LoggerFactory.getLogger(QRCodeGeneratorController.class);
    private final QRCodeGeneratorService qrCodeGeneratorService;

    @Autowired
    public QRCodeGeneratorController(QRCodeGeneratorService qrCodeGeneratorService) {
        this.qrCodeGeneratorService = qrCodeGeneratorService;
    }

    @Operation(summary = "生成二维码")
    @PostMapping("/generate")
    public ResponseEntity<QRCodeGenerator> generateQRCode(@RequestParam String qrCodeData, @RequestParam String filePath) {
        logger.info("收到生成二维码请求: data={}, filePath={}", qrCodeData, filePath);

        try {
            // 生成二维码
            QRCodeGenerator qrCodeGenerator = qrCodeGeneratorService.generateQRCode(qrCodeData, filePath, 500, 500);
            logger.info("二维码生成成功，文件路径: {}", filePath);

            // 显示二维码
            displayQRCode(filePath);

            return new ResponseEntity<>(qrCodeGenerator, HttpStatus.OK);
        } catch (WriterException | IOException e) {
            logger.error("生成二维码失败", e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Operation(summary = "生成指定尺寸的二维码")
    @PostMapping("/generate-with-size")
    public ResponseEntity<QRCodeGenerator> generateQRCodeWithSize(
            @RequestParam String qrCodeData,
            @RequestParam String filePath,
            @RequestParam int width,
            @RequestParam int height) {
        logger.info("收到生成指定尺寸二维码请求: data={}, filePath={}, width={}, height={}",
                qrCodeData, filePath, width, height);

        try {
            // 生成二维码
            QRCodeGenerator qrCodeGenerator = qrCodeGeneratorService.generateQRCode(qrCodeData, filePath, width, height);
            logger.info("指定尺寸二维码生成成功，文件路径: {}", filePath);

            // 显示二维码
            displayQRCode(filePath);

            return new ResponseEntity<>(qrCodeGenerator, HttpStatus.OK);
        } catch (WriterException | IOException e) {
            logger.error("生成指定尺寸二维码失败", e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private void displayQRCode(String filePath) {
        // 使用 SwingUtilities.invokeLater 确保在事件调度线程中执行
        SwingUtilities.invokeLater(() -> {
            try {
                logger.info("尝试显示二维码: {}", filePath);
                QRCodeDisplayUtil.displayQRCode(filePath);
            } catch (Exception e) {
                logger.error("显示二维码时出错", e);
            }
        });
    }
}